package com.zry.pda.presenter

import android.content.Intent
import android.support.v7.widget.AppCompatSpinner
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.zry.pda.R
import com.zry.pda.adapter.PackInfoAdapter
import com.zry.pda.adapter.ProductNameAdapter
import com.zry.pda.adapter.PutStorageMethodAdapter
import com.zry.pda.been.PacKBean
import com.zry.pda.been.ProductInfo
import com.zry.pda.been.PutStorageMethod
import com.zry.pda.greendao.entity.PackingOperationInfo
import com.zry.pda.retrofit.ShowUserView
import com.zry.pda.ui.activity.BatchStorageActivity
import com.zry.pda.ui.activity.PackingOperationActivity
import com.zry.pda.ui.activity.SectionNumberStorageActivity
import com.zry.pda.utils.ACacheUtil
import com.zry.pda.utils.DateTimeUtilsKt
import com.zry.pda.utils.SpinnerUtils
import com.zry.pda.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_packing_operation.*

class PackingOperationPresenter(private val activity: PackingOperationActivity) : ShowUserView {
    private var putStorageMethod: PutStorageMethod? = null
    private var productInfoBeanList: MutableList<ProductInfo>? = null
    private var productInfo: ProductInfo? = null
    private var pacKBeanList: MutableList<PacKBean>? = null
    private var pacKBean: PacKBean? = null

    fun packingOperationPresenter(view: View) {
        when (view.id) {
            R.id.packStorageBtn -> {
                if (activity.packingProportionET.text.toString().trim().isEmpty()) {
                    return ToastUtil.toast(activity, "请填写装箱比例")
                } else {
                    val intent = Intent(activity, BatchStorageActivity::class.java)
                    intent.putExtra("title", "批量装箱")
                    intent.putExtra("packingOperationInfo", getPackInfo("批量装箱"))
                    activity.startActivity(intent)
                }
            }
            R.id.packNumberBtn -> {
                if (activity.packingProportionET.text.toString().trim().isEmpty()) {
                    return ToastUtil.toast(activity, "请填写装箱比例")
                } else {
                    val intent = Intent(activity, SectionNumberStorageActivity::class.java)
                    intent.putExtra("title", "号段装箱")
                    intent.putExtra("packingOperationInfo", getPackInfo("号段装箱"))
                    activity.startActivity(intent)
                }
            }
        }
    }


    private fun getPackInfo(packType: String): PackingOperationInfo {
        val packingOperationInfo = PackingOperationInfo(
                DateTimeUtilsKt().getNowDateTime("yyyyMMddHHmmss"),
                DateTimeUtilsKt().getNowDateTime("yyyyMMddHHmmss"),
                packType, putStorageMethod!!.putStorageId,
                putStorageMethod!!.putStorageName,
                productInfo!!.ProductID,
                productInfo!!.ProductName,
                productInfo!!.UnitName,
                productInfo!!.NetContent,
                pacKBean!!.PackID,
                pacKBean!!.PackName,
                activity.packingProportionET.text.toString(),
                productInfo!!.PackSpecificationId,
                productInfo!!.PackName,
                "", "", "", packType
        )
        Log.e("TAG", packingOperationInfo.toString())
        return packingOperationInfo
    }


    fun getProductList() {
        //获取产品列表
        HttpServer(this).getProductList(ACacheUtil().getLoginInfo().EntID, activity)
        //获取装箱单位
        HttpServer(this).getPackList(activity)
    }

    fun initBoxingLevel(boxingLevelSpinner: AppCompatSpinner) {
        val putStorageMethodAdapter = PutStorageMethodAdapter(activity, SpinnerUtils().getBoxingLevel())
        boxingLevelSpinner.adapter = putStorageMethodAdapter
        spinnerItemSelect(boxingLevelSpinner)
    }

    private fun initProductName(productNameSpinner: AppCompatSpinner) {
        val productNameAdapter = ProductNameAdapter(activity, productInfoBeanList!!)
        productNameSpinner.adapter = productNameAdapter
        spinnerItemSelect(productNameSpinner)
    }

    private fun initPackingUnit(packingUnitSpinner: AppCompatSpinner) {
        val packInfoAdapter = PackInfoAdapter(activity, pacKBeanList!!)
        packingUnitSpinner.adapter = packInfoAdapter
        spinnerItemSelect(packingUnitSpinner)
    }


    private fun spinnerItemSelect(appCompatSpinner: AppCompatSpinner) {
        appCompatSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) {
                when (appCompatSpinner) {
                    activity.boxingLevelSpinner -> {
                        putStorageMethod = SpinnerUtils().getBoxingLevel()[position]
                    }
                    activity.productNameSpinner -> {
                        productInfo = productInfoBeanList!![position]
                    }
                    activity.packingUnitSpinner -> {
                        pacKBean = pacKBeanList!![position]
                    }
                }
            }

            override fun onNothingSelected(p0: AdapterView<*>?) = Unit
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            1 -> {
                val productInfoList: MutableList<ProductInfo> = data as MutableList<ProductInfo>
                productInfoBeanList = productInfoList
                initProductName(activity.productNameSpinner)
            }
            4 -> {
                val packList: MutableList<PacKBean> = data as MutableList<PacKBean>
                pacKBeanList = packList
                initPackingUnit(activity.packingUnitSpinner)
            }
        }
    }
}
